/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-19
 * Time:22:55
 */
class ListNode{
    ListNode next;
    int val;
    public ListNode(int val){
        this.val=val;
    }
}
class Linked{
    public ListNode ReverseList(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode node=head.next;
        if(node==null){
            return head;
        }
        head.next=null;
        while(node.next!=null){
            ListNode next=node.next;
            node.next=head;
            head=node;
            node=next;
        }
        node.next=head;
        return node;
    }
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        ListNode tmp=new ListNode(1);
        tmp.next=head;
        if(head==null){
            return head;
        }
        ListNode node=head;
        ListNode pre=tmp;
        int sum=1;
        while(sum<m){
            pre=node;
            node=node.next;
            sum++;
        }
        while(sum<n){
            ListNode next=node.next;
            node.next=next.next;
            next.next=pre.next;
            pre.next=next;
            sum++;
        }
        return tmp.next;
    }
}

public class Test {

}
